<?php
/**
 * Created by PhpStorm.
 * User: zyark
 * Date: 2018/2/8
 * Time: 17:27
 */

namespace app\common\controller;


use app\common\utils\WeChatUtils;
use app\index\logic\UserLogic;
use think\Config;
use think\Controller;

class UserInfoController extends Controller
{
    public function _initialize() {
    }

    public function initUserInfo(){
        $we_chat = Config::get('WeChat');
        $utils = new WeChatUtils($we_chat['app_id'], $we_chat['app_secret'], $we_chat['token']);
        if (!isset($_GET['code'])) {
            //如果没有收到CODE返回值，就发送请求
            $baseUrl = urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']);
            $utils->getUserCodeByUrl('',$baseUrl);
        }else{
            $code = $_GET['code'];
            $result = $utils->getHtmlAccessToken($code);
            $userInfo = $utils->getUserInfo($result->access_token,$result->openid);
            $logic = new UserLogic();
            $user = $logic->saveOrUpdateUser($result->openid,$userInfo);
            session('user',$user);//存入sessoin
        }
    }
}